package chatclient.tools;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

public class Filter {

    private List<FilterKey> filters;
    private boolean ignoreNonChars;
    
    public Filter(URL url, boolean ignoreNonChars)
            throws IOException {

        this.ignoreNonChars = ignoreNonChars;
        
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "iso-8859-1"));
        filters = new ArrayList<FilterKey>();

        String s;
        while ((s = br.readLine()) != null) {
  
            s = s.replace("\t", " ");
            s = s.trim(); 
            
            if (s.length() < 1) {
                continue;
            } else if (s.charAt(0) == '#') {
                continue;
            }
            
            s = s.toLowerCase();
            
            String part1 = "";
            String part2 = "";
            
            int pos = s.indexOf(" ");
            
            if(pos != -1)
            {
                part1 = s.substring(0, pos);
                part1 = part1.trim();
                
                part2 = s.substring(pos);
                part2 = Character.toString((char)27) + "G" + part2.trim() + ";";
            }
            else
            {
                System.out.println("ADZ - Filter.java->onEvent:url="+url);
                part1 = s;
                part2 = "";
                for(int i=0; i<part1.length(); i++)
                {
                    if (i < 3)
                        part2 += "*";
                    else
                        part2 += " "; // Invisible character
                }
            }
            
            filters.add(new FilterKey(part1, part2));
        }

        br.close();
        Collections.sort(filters);
    }

    public String filter(String text) {

        StringBuffer mask = null;
        
        if(ignoreNonChars)
        {
            String escape = Character.toString((char)17);
            String tmp = text.replace(escape, "");
            tmp = tmp.replaceAll("[a-zA-Z0-9]{1}", escape);          
            mask = new StringBuffer(tmp);
            text = text.replaceAll("[^a-zA-Z0-9]{1}", "");
        }

        Iterator<FilterKey> iter = filters.iterator();
        while(iter.hasNext())
        {
            FilterKey entry = iter.next();
              
            text = text.replaceAll("(?i)" + Pattern.quote(entry.getPart1()), entry.getPart2());
        }
        
        if(ignoreNonChars)
        {
           int j = 0;
           for(int i=0;i<text.length();i++)
           {
               for(; j < mask.length(); j++)
               {
                   if(mask.charAt(j) == 17)
                   {
                       mask.setCharAt(j, text.charAt(i));
                       break;
                   }
               }
           }
           
           return mask.toString();
        }
        
        return text;
    }
    
    
    public List<FilterKey> getFilterKeys()
    {
        return filters;
    }
}
